﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using DataModel;
using System.ComponentModel;
using BasicDataManagement;

namespace Kitchen
{
    /// <summary>
    /// KitchenDisplayWindow.xaml 的交互逻辑
    /// </summary>
    public partial class KitchenDisplayWindow : Window
    {
        IOpertation op;
        PosPrinter.POSPrinter printer;
        Dictionary<int, string> itemDict;
        public KitchenDisplayWindow()
        {
            InitializeComponent();
            op = OperationFactory.Create();
            printer = new PosPrinter.POSPrinter();
            itemDict = new Dictionary<int, string>();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 0, 0, 1000);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (Keyboard.FocusedElement != null)
            {
                if (!Keyboard.FocusedElement.GetType().Equals(typeof(Button)))
                {
                    if (btnPanel.Children.Count > 0)
                    {
                        btnPanel.Children[0].Focus();
                    }
                }
            }
            //if (Keyboard.FocusedElement != null)
            //{
            //    Console.WriteLine(Keyboard.FocusedElement.GetType().ToString());
            //}
            try
            {
                //读订单，打印
                var bills = op.LoadKitchenBill();
                foreach (var t in bills)
                {
                    string[] spl = t.Split(new char[] { '^' });
                    printer.PrintLine(spl[1]+"\n\n");
                    op.DeleteKitchenBill(Convert.ToInt32(spl[0]));
                }
                //读餐项
                var items = op.LoadKitchenBillItem();
                foreach (var t in items)
                {
                    string[] spl = t.Split(new char[] { '$' });
                    int id = Convert.ToInt32(spl[0]);
                    if (!itemDict.ContainsKey(id))
                    {
                        itemDict.Add(id, spl[1]);
                        //添加入按钮
                        Button tmpBtn = new Button();
                        tmpBtn.DataContext = (object)id;
                        AccessText tmpAccText = new AccessText();
                        tmpAccText.Text = spl[1];
                        tmpAccText.FontSize = 20;
                        tmpAccText.TextWrapping = TextWrapping.Wrap;
                        tmpAccText.HorizontalAlignment = HorizontalAlignment.Left;
                        tmpBtn.HorizontalContentAlignment = HorizontalAlignment.Left;
                        tmpBtn.Content = tmpAccText;
                        tmpBtn.Height = 100;
                        tmpBtn.Width = 400;
                        tmpBtn.Click += new RoutedEventHandler(tmpBtn_Click);
                        btnPanel.Children.Add(tmpBtn);
                        printer.PrintLine(spl[1]+"\n\n");
                    }
                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                MessageBox.Show("连接不到服务器，程序将自动关闭，检查配置重新开启程序！");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("程序遇到错误，请重启，若一直出现此问题，请联系开发人员");
            }
        }

        void tmpBtn_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("确认出餐？", "确认", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                int id = (int)(sender as Button).DataContext;
                op.DeleteKitchenBillItem(id);
                itemDict.Remove(id);
                btnPanel.Children.Remove(sender as Button);
            }
        }

        void mainBtn_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("确认出餐？", "确认", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                btnPanel.Children.Remove(sender as KitchenButton);
            }
        }
    }
}
